﻿using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.DataAccess.Repository
{
    public class MessagesRepository : BaseRepository
    {
        //获取我的消息列表
        public PagedList<U_Messages_EX> MyMessageLink(int id, PageView view)
        {
            string cols = @"[Name]
                          ,[ID]
                          ,[MemberId]
                          ,[Message]
                          ,[AddTime]
                          ,[MessageMemberId]
                          ,[Status]";
            string sqlWhere = " and [MemberID]=" + id;
            return base.PageGet<U_Messages_EX>(view, cols, @"View_MyMessage", sqlWhere, "[ID] ASC", "");
        }

        public List<U_Messages_EX> MyMessageDetail(string m_id, string r_id)
        {

            string sql = @"  
                        SELECT Name,
                        U_Messages.ID,
                        U_Messages.MemberId,
                        [Message],
                        MessageMemberId,
                        AddTime FROM U_Messages 
                        inner join U_Member m1 on m1.ID=U_Messages.MessageMemberId 
                        WHERE U_Messages.MemberId=@MemberId AND U_Messages.MessageMemberId=@MessageMemberId
                        order by AddTime DESC";

            return base.Query<U_Messages_EX>(sql, new { MemberId = m_id, MessageMemberId = r_id });
        }

        public int UpdateMessageStatus(string id)
        {
            string sql = "update U_Messages set Status=1 where Id=@id";

            return base.Excute(sql, new { Id = id });
        }

        public int IsNameExists(string name)
        {
            string sql = "select ID from U_Member where Name=@Name";

            if (base.Query<U_Member>(sql, new { Name = name }).Count == 0)
                return 0;
            return base.Query<U_Member>(sql, new { Name = name })[0].ID;
        }

        public int DeleteMessage(string mid, string rmid)
        {
            string sql = "delete from U_Messages where MemberId=@MemberId and MessageMemberId=@MessageMemberId";

            return base.Excute(sql, new { MemberId = mid, MessageMemberId = rmid });
        }

    }
}
